package com.medusa.aps.business.common.encrypt;

import cn.hutool.crypto.asymmetric.RSA;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 张治保
 * @since 2023/11/14
 */
@RequiredArgsConstructor
@EnableConfigurationProperties(CryptProperties.class)
public class CryptAutoconfigure {

    private final CryptProperties encryptProperties;


    @Bean
    @ConditionalOnMissingBean
    public ICrypt encryptDecrypt() {
        return new DefaultCryptImpl(
                new RSA(encryptProperties.getPrivateKey(), encryptProperties.getPublicKey())
        );
    }


    @Bean
    public CommandLineRunner encryptRunner(ICrypt encryptDecrypt) {
        return args -> CryptHelper.setEncryptDecrypt(encryptDecrypt);
    }

}
